const db = require('./db');

async function checkDatabaseConnectionAndData() {
  try {
    console.log('正在测试数据库连接...');
    
    // 测试数据库连接
    await db.testConnection();
    console.log('数据库连接成功！');
    
    // 查询教材数据
    const sql = 'SELECT id, title, author, price, images FROM textbooks ORDER BY id DESC LIMIT 10';
    const results = await db.promiseQuery(sql);
    
    console.log(`\n查询到 ${results.length} 本教材：`);
    results.forEach(textbook => {
      console.log(`\n教材 ${textbook.id}:`);
      console.log(`  标题: ${textbook.title}`);
      console.log(`  作者: ${textbook.author}`);
      console.log(`  价格: ${textbook.price}`);
      
      // 解析图片数据
      try {
        const images = JSON.parse(textbook.images || '[]');
        console.log(`  图片数量: ${images.length}`);
      } catch (e) {
        console.log('  图片数据格式错误');
      }
    });
    
  } catch (error) {
    console.error('数据库操作出错:', error.message);
  } finally {
    process.exit(0);
  }
}

checkDatabaseConnectionAndData();